﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;
using WXYW.DAL;

namespace WXYW
{
    public partial class AddProduct : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindProductType();
                BindProvider();
            }
        }

        private void BindProvider()
        {
            ProviderService providerService = new ProviderService();
            ProviderDropdownList.DataSource = providerService.GetAllSimpleProvider();
            ProviderDropdownList.DataTextField = "ProviderName";
            ProviderDropdownList.DataValueField = "ProviderID";
            ProviderDropdownList.DataBind();
        }

        private void BindProductType()
        {
            //ApplicationProductService service = new ApplicationProductService();
            ProductClassService classService = new ProductClassService();
            ProductTypeDropdownList.DataSource = classService.GetAllClass();
            ProductTypeDropdownList.DataTextField = "Name";
            ProductTypeDropdownList.DataValueField = "ID";
            ProductTypeDropdownList.DataBind();
        }

        protected void AddProductButton_Click(object sender, EventArgs e)
        {
            try
            {
                ProductService productService = new ProductService();
                productService.AddProduct(MaterialNOTxtBox.Text, ModelNOTxtBox.Text, VersionNOTxtBox.Text,
                    DescriptionTxtBox.Text, Convert.ToInt32(ProviderDropdownList.SelectedValue), Convert.ToInt32(ProductTypeDropdownList.SelectedValue),
                    Convert.ToInt32(RepairPeriodTxtBox.Text), PropertyDropdownList.SelectedValue, ContextService.CurrentAccount);
                MsgLbl.Text = "产品添加成功。";
                ClearProductForm();
            }
            catch (Exception ex)
            {
                MsgLbl.Text = string.Format("系统错误:{0}", ex.Message) ;
                LogUtility.LogUtilityIntance.LogMessgae(string.Format("异常::创建产品::{0}", ex.ToString()));
            }
        }

        private void ClearProductForm()
        {
            MaterialNOTxtBox.Text = string.Empty;
            ModelNOTxtBox.Text = string.Empty;
            DescriptionTxtBox.Text = string.Empty;
            VersionNOTxtBox.Text = string.Empty;
            RepairPeriodTxtBox.Text = string.Empty;
        }
    }
}
